﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySQLDriverCS;
namespace TJADRICD.CollaborativeDesignPlugin
{
    class MySQLAuthorize
    {
        public string authorizeUserID;
        public string majorID;
    }
    class MySQLAuthorizeDao
    {
        static String myConnstr = new MySQLConnectionString("localhost", "test", "root", "root").AsString;
        public List<MySQLAuthorize> GetAuthorize(List<string> userIdList)
        {
            List<MySQLAuthorize> authorizeList = new List<MySQLAuthorize>();
            MySQLConnection myConn = new MySQLConnection(myConnstr);
            try
            {
                myConn.Open();
                for (int i = 0; i < userIdList.Count; ++i)
                {
                    string selectstr = "select * from authorize where Auth_User_ID = '" + userIdList[i] + "'";
                    MySQLCommand myCom = new MySQLCommand(selectstr, myConn);
                    MySQLDataReader reader = myCom.ExecuteReaderEx();
                    try
                    {
                        while (reader.Read())
                        {
                            if (reader.HasRows)
                            {
                                MySQLAuthorize authorize = new MySQLAuthorize();
                                authorize.authorizeUserID = reader.GetString(1);
                                authorize.majorID = reader.GetString(2);
                                authorizeList.Add(authorize);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    reader.Close();
                }

                myConn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(myConn);
            }
            return authorizeList;
        }
        public void InsertAuthorize(List<MySQLAuthorize> authorizeList)
        {


            MySQLConnection myConn = new MySQLConnection(myConnstr);
            try
            {
                myConn.Open();
                for (int i = 0; i < authorizeList.Count; ++i)
                {
                    string insertstr = "insert into authorize values('" + authorizeList[i].authorizeUserID + "', '" + authorizeList[i].majorID + "')";
                    MySQLCommand myCom = new MySQLCommand(insertstr, myConn);
                    myCom.ExecuteNonQuery();
                }

                myConn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(myConn);
            }
        }
        public void DeleteAuthorize(List<string> userIDList)
        {

            MySQLConnection myConn = new MySQLConnection(myConnstr);
            try
            {
                myConn.Open();
                for (int i = 0; i < userIDList.Count; ++i)
                {
                    string deletestr = "delete from authorize where Auth_User_ID = '" + userIDList[i] + "'";
                    MySQLCommand myCom = new MySQLCommand(deletestr, myConn);
                    myCom.ExecuteNonQuery();
                }

                myConn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(myConn);
            }
        }
        public void UpdateAuthorize(List<MySQLAuthorize> authorizeList)
        {
            MySQLConnection myConn = new MySQLConnection(myConnstr);
            try
            {
                myConn.Open();
                for (int i = 0; i < authorizeList.Count; ++i)
                {
                    string updatestr = "update authorize set Major_ID = '" + authorizeList[i].majorID + "' where Auth_User_ID = '" + authorizeList[i].authorizeUserID + "'";
                    MySQLCommand myCom = new MySQLCommand(updatestr, myConn);
                    myCom.ExecuteNonQuery();
                }

                myConn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(myConn);
            }
        }
    }
}
